package edu.cicese.alz.swing.dialogs;

import edu.cicese.alz.swing.IntegerDocumentFilter;
import edu.cicese.alz.swing.SwingUtil;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.text.AbstractDocument;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by: Eduardo Quintana Contreras
 * Date: 14/08/12
 * Time: 06:41 PM
 */
public class InputDialog extends CustomDialog {

	JTextField txtInput;

	public InputDialog(Component owner, String title) {
		super((JFrame) SwingUtilities.getRoot(owner), title, true);

		setMin(220, 100);
		setInitialLocation();
		setResizable(false);

		initComponents();
		setVisible(true);
	}

	private void initComponents() {
		JButton btnOK = SwingUtil.createButtonBackground("OK", 71, 22, null,
				getIcon("bg"), getIcon("bgO"), getIcon("bgP"));
		btnOK.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				actionOK();
			}
		});
		JButton btnCancel = SwingUtil.createButtonBackground("Cancel", 71, 22, null,
				getIcon("bg"), getIcon("bgO"), getIcon("bgP"));
		btnCancel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				actionCancel();
			}
		});

		txtInput = SwingUtil.createTextField(60, 24);
		((AbstractDocument) txtInput.getDocument()).setDocumentFilter(new IntegerDocumentFilter(txtInput));

		Box boxButtons = Box.createHorizontalBox();
		boxButtons.add(Box.createHorizontalGlue());
		boxButtons.add(btnOK);
		boxButtons.add(Box.createHorizontalStrut(10));
		boxButtons.add(btnCancel);
		boxButtons.add(Box.createHorizontalGlue());

		Box boxInput = Box.createHorizontalBox();
		boxInput.add(Box.createHorizontalGlue());
		boxInput.add(new JLabel("Number of subjects:"));
		boxInput.add(Box.createHorizontalStrut(3));
		boxInput.add(txtInput);
		boxInput.add(Box.createHorizontalGlue());

		Box boxContent = Box.createVerticalBox();
		boxContent.add(Box.createVerticalStrut(4));
		boxContent.add(boxInput);
		boxContent.add(Box.createVerticalStrut(8));
		boxContent.add(boxButtons);
		boxContent.add(Box.createVerticalGlue());

		JPanel pContent = new JPanel();
		pContent.add(boxContent);
		pContent.setBackground(Color.WHITE);
		setContentPane(pContent);
	}

	public void actionOK() {
		if (!txtInput.getText().trim().equals("")) {
			setOk(true);
			setVisible(false);
		}
	}

	public int getQuantity() {
		try {
			return Integer.valueOf(txtInput.getText());
		}
		catch (NumberFormatException e) {
			return -1;
		}
	}

	private void actionCancel() {
		closeAndDispose();
	}

	public static void main(String[] a) {
		new InputDialog(new JFrame(), "Confirmation").setVisible(true);
	}
}
